﻿Imports System.Text.RegularExpressions
Imports System.Text
'Imports MySql.Data
Imports MySql.Data.MySqlClient
Imports System.IO


Public Class adopcion

    Dim connectionstring As String
    Dim commandtext As String
    'Private mydataset As DataSet
    Dim adapter As MySqlDataAdapter
    Dim adapterSerch As MySqlDataAdapter
    Dim table As DataTable
    Dim conexion As MySqlConnection

    Dim codAnimal, nomAnimal, tipoAnimal, fechaRegistro, fechaAdopcion, obsAnimal As String
    Dim codOrigen, nomOrigen, apelOrigen, telOrigen, mailOrigen As String
    Dim codAdopt, nomAdop, apelAdop, telAdop, mailAdop As String

    Private Sub adopcion_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Form1.Show()
    End Sub
    Private Sub adopcion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        connectionstring = "server=localhost;user id=root;password=root;database=zoo;"
        commandtext = "select a.id as idA, a.nombre as mascotas, a.fechregistro,a.fechadopcion, a.observaciones, d.id as idD,d.nombre as nomDon,d.apellidos as apeDon,d.telefono as telDon,d.mail as mailDon,ad.id as idAd,ad.nombre as nomAdo,ad.apellidos as apeAdo,ad.dni,ad.telefono as telAdo,ad.mail as mailAdo  from donante d inner join animal a on d.id=a.donante inner join adoptante ad on a.adoptante=ad.id"
        Try
            adapter = New MySqlDataAdapter(commandtext, connectionstring)
            table = New DataTable
            adapter.Fill(table)

            dgv_adop.DataSource = table
            dgv_adop.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
            registro()

            txt_codanimal.DataBindings.Add("Text", table, "idA")
            txt_nommascota.DataBindings.Add("Text", table, "mascotas")
            dtp_registro.DataBindings.Add("Text", table, "fechregistro")
            dtp_adopcion.DataBindings.Add("Text", table, "fechadopcion")
            txt_obsAnimal.DataBindings.Add("Text", table, "observaciones")
            txt_codOrigen.DataBindings.Add("Text", table, "idD")
            txt_nomOrigen.DataBindings.Add("Text", table, "nomDon")
            txt_apelOrigen.DataBindings.Add("Text", table, "apeDon")
            txt_telOrigen.DataBindings.Add("Text", table, "telDon")
            txt_mailorigen.DataBindings.Add("Text", table, "mailDon")
            txt_codAdopt.DataBindings.Add("Text", table, "idAd")
            txt_nomAdop.DataBindings.Add("Text", table, "nomAdo")
            txt_apelAdop.DataBindings.Add("Text", table, "apeAdo")
            txt_dniAdop.DataBindings.Add("Text", table, "dni")
            txt_telAdop.DataBindings.Add("Text", table, "telAdo")
            txt_mailadopt.DataBindings.Add("Text", table, "mailAdo")
        Catch ex As Exception
            MsgBox("Error en la base de datos")
            Dim fs As New FileStream(".\logzoo.txt", FileMode.Append, FileAccess.Write)
            Dim sw As New StreamWriter(fs)
            sw.WriteLine(ex.Message.ToString)
            sw.Close()
            fs.Close()
        End Try

    End Sub

    Private Sub txt_codanimal_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_codanimal.KeyPress
        validarNumero(e, txt_codanimal)
    End Sub

    Private Sub txt_codanimal_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_codanimal.Leave
        codAnimal = txt_codanimal.Text
    End Sub

    Private Sub txt_nommascota_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_nommascota.KeyPress
        validarNombre(e, txt_nommascota)
    End Sub

    Private Sub txt_nommascota_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_nommascota.Leave
        nomAnimal = txt_nommascota.Text
    End Sub

    Private Sub dtp_registro_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtp_registro.ValueChanged
        fechaRegistro = dtp_registro.Value.ToString
    End Sub

    Private Sub dtp_adopcion_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtp_adopcion.ValueChanged
        fechaAdopcion = dtp_adopcion.Value.ToString
    End Sub

    Private Sub txt_obsAnimal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_obsAnimal.TextChanged
        obsAnimal = Trim(txt_obsAnimal.Text)
    End Sub

    Private Sub txt_codOrigen_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_codOrigen.KeyPress
        validarNumero(e, txt_codOrigen)
    End Sub

    Private Sub txt_codOrigen_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_codOrigen.Leave
        codOrigen = txt_codOrigen.Text
    End Sub

    Private Sub txt_nomOrigen_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_nomOrigen.KeyPress
        validarNombre(e, txt_nomOrigen)
    End Sub

    Private Sub txt_nomOrigen_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_nomOrigen.Leave
        nomOrigen = Trim(txt_nomOrigen.Text)
    End Sub

    Private Sub txt_apelOrigen_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txt_apelOrigen.KeyPress
        ValidarApellidos(e, txt_apelOrigen)
    End Sub

    Private Sub txt_apellorigen_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_apelOrigen.Leave
        apelOrigen = Trim(txt_apelOrigen.Text)
    End Sub

    Private Sub txt_telOrigen_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_telOrigen.KeyPress
        validarNumero(e, txt_telOrigen)
    End Sub

    Private Sub txt_telOrigen_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_telOrigen.Leave
        telOrigen = txt_telOrigen.Text
    End Sub

    Private Sub txt_mailorigen_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_mailorigen.Leave
        'SOLO PERMITE DEJAR EL TEXTBOX SI ESTA VALIDADO O NO HAY TEXTO
        If Not (txt_mailorigen.Text = Nothing) Then
            If ValidarCorreo(txt_mailorigen.Text, txt_mailorigen) Then
                mailOrigen = Trim(txt_mailorigen.Text)
                ErrorProvider1.SetError(txt_mailorigen, Nothing)
            Else
                ErrorProvider1.SetError(txt_mailorigen, "Formato erroneo")
                txt_mailorigen.Focus()
            End If
        End If
    End Sub

    Private Sub txt_codAdopt_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_codAdopt.KeyPress
        validarNumero(e, txt_codAdopt)
    End Sub

    Private Sub txt_codAdopt_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_codAdopt.Leave
        codAdopt = txt_codAdopt.Text
    End Sub

    Private Sub txt_nomAdop_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_nomAdop.KeyPress
        validarNombre(e, txt_nomAdop)
    End Sub

    Private Sub txt_nomAdop_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_nomAdop.Leave
        nomAdop = txt_nomAdop.Text
    End Sub

    Private Sub txt_apelAdop_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_apelAdop.KeyPress
        ValidarApellidos(e, txt_apelAdop)

    End Sub

    Private Sub txt_apelAdop_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_apelAdop.Leave
        apelAdop = txt_apelAdop.Text
    End Sub

    Private Sub txt_dniAdop_Leave(sender As Object, e As System.EventArgs) Handles txt_dniAdop.Leave
        Dim esNIF As Boolean = Verificar_DNI(txt_dniAdop.Text)
        If esNIF Then
            ErrorProvider1.SetError(txt_dniAdop, Nothing)
        Else
            ErrorProvider1.SetError(txt_dniAdop, "Formato incorreccto")
            txt_dniAdop.Focus()
        End If
    End Sub

    Private Sub txt_telAdop_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_telAdop.KeyPress
        validarNumero(e, txt_telAdop)
    End Sub

    Private Sub txt_telAdop_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_telAdop.Leave
        telAdop = txt_telAdop.Text
    End Sub

    Private Sub txt_mailadopt_Leave(sender As Object, e As System.EventArgs) Handles txt_mailadopt.Leave
        If Not (txt_mailadopt.Text = Nothing) Then
            If ValidarCorreo(txt_mailadopt.Text, txt_mailadopt) Then
                mailAdop = Trim(txt_mailadopt.Text)
            Else
                txt_mailorigen.Focus()
            End If
        End If
    End Sub

    Private Sub SalirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalirToolStripMenuItem.Click
        Form1.Show()
        Me.Close()
    End Sub

    Private Sub AltaToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AltaToolStripMenuItem.Click
        limpiarCajas()
    End Sub
    ''  FUNCIONES ''
    ''''''''''''''''
    ''' <summary>
    ''' VALIDA QUE EL FORMATO DEL EMAIL SEA CORRECTO,
    ''' QUE CONTENGA UN @ Y UN PUNTO SEGUIDO DE ENTRE
    ''' DOS Y TRES CARACTERES.
    ''' </summary>
    ''' <param name="correo">CADENA A SER EVALUADA</param>
    ''' <param name="txtbox">EL TEXTBOS AL QUE SE DIRIGE EL ERRORPROVIDER</param>
    ''' <returns>DEVUELVE TRUE SI EL FORMATO ES CORRECTO</returns>
    ''' <remarks></remarks>
    Public Function ValidarCorreo(ByVal correo As String, ByVal txtbox As TextBox) As Boolean
        'DESPUES DE IMPORTAR: System.Text.RegularExpressions
        Dim re As New Regex("\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+")
        Dim valido As Boolean
        If re.IsMatch(correo) Then
            valido = True
            ErrorProvider1.SetError(txtbox, Nothing)
        Else
            ErrorProvider1.SetError(txtbox, "Sólo admite letras y espacios")
            valido = False
        End If
        re = Nothing
        Return valido
    End Function

    ''' <summary>
    ''' VALIDA QUE EL TEXTO INTRODUCIDO SOLO CONTENGA
    ''' LETRAS Y ESPACIOS EN BLANCO. Y QUITA LOS ESPACIOS
    ''' EN BLANCO QUE SOBRAN
    ''' </summary>
    ''' <param name="e">EVENTO DE TECLA PRECIONADA</param>
    ''' <param name="txtbox">EL TEXTBOX AL QUE SE DIRIGE EL ERRORPROVIDER</param>
    ''' <remarks></remarks>
    Public Sub ValidarApellidos(ByVal e As System.Windows.Forms.KeyPressEventArgs, ByVal txtbox As TextBox)
        'QUITA ESPACIOS EN BLANCO AL PRINCIPIO Y SE ASEGURA QUE SOLO HAYA UNO EN BLANCO ENTRE PALABRAS
        If (Char.IsDigit(e.KeyChar)) Then
            e.Handled = True
            ErrorProvider1.SetError(txtbox, "Sólo admite letras y espacios")
        ElseIf (Char.IsNumber(e.KeyChar)) Then
            e.Handled = True
            ErrorProvider1.SetError(txtbox, "Sólo admite letras y espacios")
        ElseIf (Char.IsSymbol(e.KeyChar)) Then
            e.Handled = True
            ErrorProvider1.SetError(txtbox, "Sólo admite letras y espacios")
        ElseIf (Char.IsPunctuation(e.KeyChar)) Then
            e.Handled = True
            ErrorProvider1.SetError(txtbox, "Sólo admite letras y espacios")
        Else
            If txtbox.Text.StartsWith(" ") Then
                txtbox.Text = txtbox.Text.Trim
                ErrorProvider1.SetError(txtbox, Nothing)
            ElseIf txtbox.Text.EndsWith(" ") Then
                txtbox.Text = txtbox.Text.Trim + " "
                txtbox.SelectionStart = txtbox.Text.Length
            End If
            ErrorProvider1.SetError(txtbox, Nothing)
        End If
    End Sub

    Public Sub validarNombre(ByVal e As System.Windows.Forms.KeyPressEventArgs, ByVal txtbox As TextBox)
        'SOLO ADMITE LETRAS Y ESPACIOS
        If (Char.IsDigit(e.KeyChar)) Then
            e.Handled = True
            ErrorProvider1.SetError(txtbox, "Sólo admite letras y espacios")
        ElseIf (Char.IsNumber(e.KeyChar)) Then
            e.Handled = True
            ErrorProvider1.SetError(txtbox, "Sólo admite letras y espacios")
        ElseIf (Char.IsSymbol(e.KeyChar)) Then
            e.Handled = True
            ErrorProvider1.SetError(txtbox, "Sólo admite letras y espacios")
        ElseIf (Char.IsPunctuation(e.KeyChar)) Then
            e.Handled = True
            ErrorProvider1.SetError(txtbox, "Sólo admite letras y espacios")
        Else
            If txtbox.Text.StartsWith(" ") Then
                txtbox.Text = txtbox.Text.Trim
                ErrorProvider1.SetError(txtbox, Nothing)
            ElseIf txtbox.Text.EndsWith(" ") Then
                txtbox.Text = txtbox.Text.Trim + " "
                txtbox.SelectionStart = txtbox.Text.Length
            End If
            ErrorProvider1.SetError(txtbox, Nothing)
        End If
    End Sub

    Private Sub validarNumero(ByVal e As System.Windows.Forms.KeyPressEventArgs, ByVal txtbox As TextBox)
        If e.KeyChar < "0" Or e.KeyChar > "9" Then
            ErrorProvider1.SetError(txtbox, "Sólo admite numerales")
            If e.KeyChar <> Chr(8) Then
                e.KeyChar = Chr(0)
                ErrorProvider1.SetError(txtbox, "Sólo admite numerales")
            Else
                ErrorProvider1.SetError(txtbox, Nothing)
            End If
        Else
            ErrorProvider1.SetError(txtbox, Nothing)
        End If
    End Sub

    Private Function CalculaLetraDNI(ByVal cadena As String) As String
        ' CALCULA LA LETRA DEL DNI Y DELVUELVE EL DNI CORRECTO
        Const myCadena As String = "TRWAGMYFPDXBNJZSQVHLCKE"
        Const myNumeros As String = "0123456789"
        Dim a, b, c, NIF As Integer
        Dim sb As New StringBuilder
        cadena = Trim(cadena)
        If Len(cadena) = 0 Then Return ""
        ' SELECCIONA SOLO LOS NUMEROS
        For i As Integer = 0 To cadena.Length - 1
            If myNumeros.IndexOf(cadena(i)) > -1 Then
                sb.Append(cadena(i))
            End If
        Next
        cadena = sb.ToString
        a = 0
        NIF = CInt(Val(cadena))
        Do
            b = CInt(Int(NIF / 24))
            c = NIF - (24 * b)
            a = a + c
            NIF = b
        Loop While b <> 0
        b = CInt(Int(a / 23))
        c = a - (23 * b)
        Return cadena & Mid(myCadena, CInt(c + 1), 1)
    End Function

    Public Function Verificar_DNI(ByVal cadena As String) As Boolean
        ' COMPRUEBA SI EL DNI ES VALIDO Y DEVUELVE TRUE SI ES CORRECTO
        Dim valido As Boolean
        Dim aux As String
        cadena = cadena.ToUpper 'CAMBIA LA LETRA A MAYUSCULAS
        aux = cadena.Substring(0, cadena.Length - 1) 'SELECCIONA SOLO LOS NUMEROS
        If aux.Length >= 7 AndAlso IsNumeric(aux) Then
            aux = CalculaLetraDNI(aux) 'CALCULA LA LETRA DEL DNI A PARTIR DE LOS NUMEROS
            If cadena <> aux Then 'COMPARA LA LETRA CALCULADA CON LA INGRESADA
                valido = False
            Else
                valido = True
            End If
        End If
        Return valido
    End Function

    Public Sub limpiarCajas()
        txt_codanimal.Clear()
        txt_nommascota.Clear()
        txt_obsAnimal.Clear()
        txt_codOrigen.Clear()
        txt_nomOrigen.Clear()
        txt_apelOrigen.Clear()
        txt_telOrigen.Clear()
        txt_mailorigen.Clear()
        txt_codAdopt.Clear()
        txt_nomAdop.Clear()
        txt_apelAdop.Clear()
        txt_dniAdop.Clear()
        txt_telAdop.Clear()
        txt_mailadopt.Clear()
    End Sub

    Private Sub registro()
        'lbl_cont.Text = ((Me.BindingContext(table).Position + 1).ToString + " de ") + BindingContext(table).Count.ToString
    End Sub

    'Private Sub GuardarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles GuardarToolStripMenuItem.Click
    '    guardarDatos(txt_dniCli.Text, txt_codPac.Text)
    'End Sub

    Private Function consultaSiExsisten(ByVal pk As String, ByVal tabla As String, ByVal valor As String) As Boolean
        Dim rd As MySqlDataReader
        Dim cmd As New MySqlCommand
        Dim xresult As String = Nothing
        conexion = New MySqlConnection(connectionstring)
        conexion.Open()
        cmd.CommandText = "SELECT " + pk + " FROM " + tabla + " WHERE " + pk + "=@id"
        cmd.Parameters.AddWithValue("@id", valor)
        cmd.Connection = conexion
        rd = cmd.ExecuteReader
        If rd.Read Then
            xresult = rd.GetString(0)
        End If
        conexion.Close()
        If xresult = valor Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Function idMascota(ByVal idMasc As String) As String
        Dim rd As MySqlDataReader
        Dim cmd As New MySqlCommand
        Dim xresult As String = Nothing
        conexion = New MySqlConnection(connectionstring)
        conexion.Open()
        cmd.CommandText = "SELECT id FROM mascota where id=@id"
        cmd.Parameters.AddWithValue("@id", idMasc)
        cmd.Connection = conexion
        rd = cmd.ExecuteReader
        If rd.Read Then
            Return xresult = rd.GetString(0)
        End If
        conexion.Close()
        Return xresult
    End Function

    'Private Sub guardarDatos(ByVal dni As String, ByVal codpac As String)
    '    If txt_dniCli.TextLength > 0 Then
    '        Dim existeCliente As Boolean = consultaSiExsisten("dni", "cliente", dni)
    '        Select Case existeCliente
    '            Case True
    '                Dim exsisteMascota As Boolean = consultaSiExsisten("id", "mascota", codpac)
    '                Select Case exsisteMascota
    '                    Case True
    '                        If idMascota(txt_codPac.Text) <> "NO" Then
    '                            guardarHistorial(idMascota(txt_codPac.Text))
    '                        Else
    '                            MsgBox("Error en integridad de la base de datos. Consulte con el servicio técnico.", vbCritical, "Error en la Base de Datos")
    '                        End If
    '                    Case False
    '                        guardarMascota(codpac)
    '                        Dim xcommandtext As String = ("SELECT MAX(id) FROM mascota")
    '                        Dim xmda As New MySqlDataAdapter(commandtext, connectionstring)
    '                        Dim xtable As New DataTable
    '                        xmda.Fill(xtable)
    '                        Dim cadena As New TextBox
    '                        cadena.DataBindings.Add("text", xtable, "mascota.id")
    '                        guardarHistorial(cadena.Text)
    '                End Select
    '            Case False
    '                guardarCliente()
    '                guardarMascota(codpac)
    '                Dim xcommandtext As String = ("SELECT MAX(id) FROM mascota")
    '                Dim xmda As New MySqlDataAdapter(commandtext, connectionstring)
    '                Dim xtable As New DataTable
    '                xmda.Fill(xtable)
    '                Dim cadena As New TextBox
    '                cadena.DataBindings.Add("text", xtable, "mascota.id")
    '                guardarHistorial(cadena.Text)
    '        End Select
    '    Else
    '        MsgBox("El campo de DNI de Cliente es obligatorio", vbCritical, "ERROR")
    '    End If
    '    table.Clear()
    '    adapter.Fill(table)
    '    DataGridView1.DataSource = table
    '    DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
    'End Sub
    'Private Sub guardarCliente()
    '    Try
    '        conexion = New MySqlConnection(connectionstring)
    '        Dim insertCli As MySqlCommand
    '        Dim insert As String = "INSERT INTO cliente( dni,nombre,apellidos, direccion,telefono,ciudad,pais,codpostal,email) Values(@dni,@nombre,@apellidos,@direccion,@telefono,@ciudad,@pais,@codpostal,@email)"
    '        insertCli = New MySqlCommand(insert, conexion)
    '        With insertCli.Parameters
    '            .AddWithValue("@dni", txt_dniCli.Text)
    '            .AddWithValue("@nombre", nomCli)
    '            .AddWithValue("@apellidos", apeCli)
    '            .AddWithValue("@direccion", dirCli)
    '            .AddWithValue("@telefono", telCli)
    '            .AddWithValue("@ciudad", ciuCli)
    '            .AddWithValue("@pais", paisCli)
    '            .AddWithValue("@codpostal", codPosCli)
    '            .AddWithValue("@email", mailCli)
    '        End With
    '        conexion.Open()
    '        insertCli.ExecuteNonQuery()
    '    Catch ex As Exception
    '        MsgBox("Error al guardar Clente " + ex.Message.ToString, vbCritical, "ERROR")
    '        Dim fs As New FileStream(".\logzoo.txt", FileMode.Append, FileAccess.Write)
    '        Dim sw As New StreamWriter(fs)
    '        sw.WriteLine("Error al guardar Clente " + ex.Message.ToString)
    '        sw.Close()
    '        fs.Close()
    '        conexion.Close()
    '        conexion.Close()
    '        conexion.Close()
    '    Finally
    '        conexion.Close()
    '    End Try
    'End Sub

    'Private Sub guardarMascota(ByVal pkClente As String)
    '    Try
    '        conexion = New MySqlConnection(connectionstring)
    '        Dim insertMas As MySqlCommand
    '        Dim ordenMascota As String = "INSERT INTO mascota(nombre,tipo,raza,duenio)VALUES(@nombre,@tipo,@raza,@duenio)"
    '        insertMas = New MySqlCommand(ordenMascota, conexion)
    '        insertMas.Parameters.AddWithValue("@nombre", nomPac)
    '        insertMas.Parameters.AddWithValue("@tipo", tipoAnimal)
    '        insertMas.Parameters.AddWithValue("@raza", razPac)
    '        insertMas.Parameters.AddWithValue("@duenio", dniCli)

    '        conexion.Open()
    '        insertMas.ExecuteNonQuery()

    '    Catch ex As Exception
    '        MsgBox("Error al guardar Mascota " + ex.Message.ToString, vbCritical, "ERROR")
    '        Dim fs As New FileStream(".\logzoo.txt", FileMode.Append, FileAccess.Write)
    '        Dim sw As New StreamWriter(fs)
    '        sw.WriteLine("Error al guardar Mascota " + ex.Message.ToString)
    '        sw.Close()
    '        fs.Close()
    '        conexion.Close()
    '        conexion.Close()
    '    Finally
    '        conexion.Close()
    '    End Try
    'End Sub

    'Private Sub guardarHistorial(ByVal pkMascota As String)
    '    Try
    '        conexion = New MySqlConnection(connectionstring)
    '        Dim insertHist As MySqlCommand
    '        Dim oredenHistorial As String = "INSERT INTO historial(ingreso,alta,motivo,diagnostico,tratamiento,mascota)VALUES(@ingreso,@alta,@motivo,@diagnostico,@tratamiento,@mascota)"
    '        insertHist = New MySqlCommand(oredenHistorial, conexion)
    '        With insertHist
    '            Try
    '                .Parameters.AddWithValue("@ingreso", dtp_fechIng.Text)
    '                .Parameters.AddWithValue("@alta", dtp_fechAlt.Text)
    '            Catch ex As Exception
    '                MsgBox("Error al guaradar FECHA historial", vbCritical, "ERROR")
    '                Dim fs As New FileStream(".\logzoo.txt", FileMode.Append, FileAccess.Write)
    '                Dim sw As New StreamWriter(fs)
    '                sw.WriteLine("Error al guaradar FECHA historial" + ex.Message.ToString)
    '                sw.Close()
    '                fs.Close()
    '            End Try
    '            .Parameters.AddWithValue("@motivo", motIng)
    '            .Parameters.AddWithValue("@diagnostico", diag)
    '            .Parameters.AddWithValue("@tratamiento", trat)
    '            .Parameters.AddWithValue("@mascota", Val(pkMascota))
    '        End With

    '        conexion.Open()
    '        insertHist.ExecuteNonQuery()

    '    Catch ex As Exception
    '        MsgBox("Error al guardar Historial " + ex.Message.ToString, vbCritical, "ERROR")
    '        Dim fs As New FileStream(".\logzoo.txt", FileMode.Append, FileAccess.Write)
    '        Dim sw As New StreamWriter(fs)
    '        sw.WriteLine("Error al guardar Historial " + ex.Message.ToString)
    '        sw.Close()
    '        fs.Close()
    '        conexion.Close()
    '    Finally
    '        conexion.Close()
    '    End Try
    'End Sub
End Class

